package com.example.surveyapplication;

import help.Define;
import help.Utility;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SignInActivity extends Activity implements OnClickListener {
	Button btnSignIn;
	Button btnSignUp;
	TextView lblForgetPassword;
	EditText txtEmail;
	EditText txtPassword;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sign_in);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);		
		initComponents();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	        moveTaskToBack(false);
	        return false;
	    }
	    return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.sign_in, menu);
		return true;
	}
	
	private void initComponents() {
		btnSignIn = (Button)findViewById(R.id.btnSignIn_SignIn);
		btnSignUp = (Button)findViewById(R.id.btnSignUp_SignIn);
		lblForgetPassword = (TextView)findViewById(R.id.lblForgetPassword_SignIn);
		txtEmail = (EditText)findViewById(R.id.txtEmail_SignIn);
		txtPassword = (EditText)findViewById(R.id.txtPassword_SignIn);
		
		btnSignIn.setOnClickListener(this);
		btnSignUp.setOnClickListener(this);
		lblForgetPassword.setOnClickListener(this);
	}

	public void onClick(View view) {
		Intent intent;
		switch (view.getId())
		{
		case R.id.btnSignIn_SignIn:
			String email = txtEmail.getText().toString();
			String password = txtPassword.getText().toString();
			
			switch (Utility.signIn(email, password)) {
			case Define.kEmpty:
				showAlert("Email or Password is empty.");
				break;
			case Define.kFail:
				showAlert("Email or Password is wrong.");
				break;
			case Define.kSuccess:
				intent = new Intent(this, PersonalInfoActivity.class);
				startActivity(intent);
				break;
			default:
				break;
			}
			break;
		case R.id.btnSignUp_SignIn:
			intent = new Intent(this, SignUpActivity.class);
			startActivity(intent);
			break;
		case R.id.lblForgetPassword_SignIn:
			intent = new Intent(this, ForgetPasswordActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}
	
	private void showAlert(String message) {
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("");
		alertDialog.setMessage(message);
		alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				}
				});
		alertDialog.show();
	}
}
